local daomeng = fk.CreateSkill {
  name = "lvshi__daomeng",
}

Fk:loadTranslationTable{
  ["lvshi__daomeng"] = "蹈盟",
  [":lvshi__daomeng"] = "弃牌阶段，你可改为和一名己方角色交换手牌，然后手牌数较多的角色视为使用【杀】，手牌数较少的角色获得弃牌堆内一张【闪】。",
  ["#lvshi__daomeng-target"] = "蹈盟：你可以和一名己方角色交换手牌",

  ["$lvshi__daomeng_lvshi__lusu1"] = "联刘以抗曹，此可行之大势。",
  ["$lvshi__daomeng_lvshi__lusu2"] = "强敌在北，唯协力可御之。",

  ["$ls__daomeng_lvshi__zhugejin1"] = "友邦和睦，民安阖乐。",
  ["$ls__daomeng_lvshi__zhugejin2"] = "盟友相护，毋需担忧。",
}
local ls = require "packages/lvshi/utility"

daomeng:addEffect(fk.EventPhaseStart, {
  mute = true,
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(daomeng.name) and player.phase == Player.Discard
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    local to = room:askToChoosePlayers(player, {
      min_num = 1,
      max_num = 1,
      targets = table.filter(room:getOtherPlayers(player), function (p)
        return ls.isFriend(player, p) end),
      skill_name = daomeng.name,
      prompt = "#lvshi__daomeng-target",
      cancelable = true,
    })
    if #to > 0 then
      event:setCostData(self, {tos = to})
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    data.phase_end = true
    local to = event:getCostData(self).tos[1]
    room:swapAllCards(player, {player, to}, daomeng.name)
    if player:getHandcardNum() ~= to:getHandcardNum() then
      local user,other
      if player:getHandcardNum() > to:getHandcardNum() then
        user = player
        other = to
      elseif player:getHandcardNum() < to:getHandcardNum() then
        user = to
        other = player
      end
      if user then
        room:askToUseVirtualCard(user, {
          name = "slash",
          skill_name = daomeng.name,
        })
      end
      if other then
        local jinks = table.filter(room.discard_pile, function (id) return Fk:getCardById(id).trueName == "jink" end)
        if #jinks > 0 then
          local jink = table.random(jinks)
          room:obtainCard(other, jink, true, fk.ReasonJustMove)
        end
      end
    end
  end,
})

return daomeng